package com.framework.sog.advice;

import java.lang.reflect.Method;

import com.framework.bas.advice.AbsBeforeAdvice;
import com.framework.bas.bs.IPlmBasBS;
import com.framework.bas.param.GeneralParam;
import com.framework.bas.service.IControl;
import com.framework.bas.util.PlmBasConstant;
import com.framework.bas.util.UtilWeb;
import com.framework.bas.vo.BasService;
import com.framework.sog.bs.IPlmSogBS;
import com.framework.sog.vo.HpSogServiceVO;

public class ServiceBeforeLog extends AbsBeforeAdvice {
	
	private IPlmSogBS plmSogBS;
	private IPlmBasBS plmBasBS;

	public IPlmSogBS getPlmSogBS() {
		return plmSogBS;
	}

	public void setPlmSogBS(IPlmSogBS plmSogBS) {
		this.plmSogBS = plmSogBS;
	}

	public IPlmBasBS getPlmBasBS() {
		return plmBasBS;
	}

	public void setPlmBasBS(IPlmBasBS plmBasBS) {
		this.plmBasBS = plmBasBS;
	}

	public void runAdvice(Method method, Object[] args, Object obj)
			throws Throwable {
		boolean paramFlag = false;
		int pos = 0;
		if ((args!=null) && (obj instanceof IControl)){
			for(int i=0 ; i<args.length; i++){
				if(args[i] instanceof GeneralParam){
					paramFlag = true;
					pos = i;
					break;
				}
			}
			if(paramFlag){
				GeneralParam param = (GeneralParam) args[pos];
				String serviceCode = param.getAppService();
				if(param.getValueObject(PlmBasConstant.APP_SERVICE_OBJECT) == null){
					//封装交易信息
					BasService hpBasServiceVO = getPlmBasBS().queryServiceByCode(serviceCode);
					param.setValue(PlmBasConstant.APP_SERVICE_OBJECT, hpBasServiceVO);
				}
				if(param.getValueString(PlmBasConstant.APP_SYS_FLAG) == null){
					//封装系统标记
					param.setValue(PlmBasConstant.APP_SYS_FLAG, UtilWeb.getSysFlag());					
				}
				//判断该交易是否记录日志
				//保存系统交易日志信息
				HpSogServiceVO vo =new HpSogServiceVO();
				serviceCode = param.getAppService();
				vo.setServiceCode(serviceCode);
				vo.setRunStatus(PlmBasConstant.SERVICE_HANDLE_START);
			}
		}
	}

}
